﻿#region

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

#endregion

namespace Radovici.Ctrls.ContentControls
{
    public class Sizable : ContentControl
    {
        #region Private fields        

        private bool _isSizing;

        #endregion

        public Sizable()
        {
            DefaultStyleKey = typeof (Sizable);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var sz = GetTemplateChild("SizeBottomRight") as UIElement;
            if (sz != null)
            {
                sz.MouseLeftButtonDown += OnSizeLeftButtonDown;
                sz.MouseMove += OnSizeMouseMove;
                sz.MouseLeftButtonUp += OnSizeMouseLeftButtonUp;
            }
            //var scrollable = this.GetTemplateChild("scrollable") as ScrollViewer;
            //if (scrollable != null)
            //{
            //    scrollable.HorizontalScrollBarVisibility =
            //        scrollable.VerticalScrollBarVisibility =
            //        ScrollingEnabled ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden;
            //}
        }

        private void OnSizeMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isSizing)
            {
                var ctrl = sender as UIElement;
                if (ctrl != null)
                {
                    ctrl.ReleaseMouseCapture();
                    _isSizing = false;
                    e.Handled = true;
                }
            }
        }

        private void OnSizeMouseMove(object sender, MouseEventArgs e)
        {
            if (_isSizing)
            {
                var ctrl = sender as UIElement;
                if (ctrl != null)
                {
                    Point curPosition = e.GetPosition(this);
                    double expHeight = curPosition.Y;
                    double minHeight = double.IsNaN(MinHeight) ? 20 : MinHeight;
                    double expWidth = curPosition.X;
                    double minWidth = double.IsNaN(MinWidth) ? 20 : MinWidth;
                    Height = (expHeight < minHeight) ? minHeight : expHeight;
                    Width = (expWidth < minWidth) ? minWidth : expWidth;
                }
            }
        }

        private void OnSizeLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (SizingEnabled && !_isSizing)
            {
                var ctrl = sender as UIElement;
                if (ctrl != null)
                {
                    ctrl.CaptureMouse();
                    _isSizing = true;
                    e.Handled = true;
                }
            }
        }

        #region SizingEnabled

        // Using a DependencyProperty as the backing store for SizingEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SizingEnabledProperty = DependencyProperty.Register("SizingEnabled",
                                                                                                      typeof (bool),
                                                                                                      typeof (Sizable),
                                                                                                      new PropertyMetadata
                                                                                                          (true));

        public bool SizingEnabled
        {
            get { return (bool) GetValue(SizingEnabledProperty); }
            set { SetValue(SizingEnabledProperty, value); }
        }

        #endregion
    }
}